//
//  RegisterController.m
//  i5db
//
//  Created by iMuse on 15-5-19.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "RegisterController.h"

#import "IdentifyingController.h"
#import "AppCenter.h"
#import <SMS_SDK/SMS_SDK.h>

@interface RegisterController ()
{
    UITextField * userNameField;
    UITextField * passwordField;
    UIButton    * registerButton;
    UILabel     * line;
    NSMutableArray * _areaArray;
    
}

@end

@implementation RegisterController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"填写手机号";
    }
    return self;
}
-(void) initNavigationBar
{
    [super initNavigationBar];
}
-(void)updateViewConstraints
{
    [super updateViewConstraints];
    
    [userNameField  autoPinToTopLayoutGuideOfViewController:self withInset:10];
    [userNameField autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [userNameField autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [userNameField autoSetDimension:ALDimensionHeight toSize:45];
    
    [registerButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:userNameField withOffset:10];
    [registerButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20];
    [registerButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:20];
    [registerButton autoSetDimension:ALDimensionHeight toSize:40];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setNeedsUpdateConstraints];
    [self initNavigationBar];
    [self initView];
}

-(void) initView
{
    userNameField = [UITextField newAutoLayoutView];
    userNameField.textAlignment = NSTextAlignmentCenter;
    userNameField.placeholder = @"手机号";
    userNameField.keyboardType = UIKeyboardTypeNumberPad;
    userNameField.backgroundColor = [UIColor whiteColor];
   
    [self.view addSubview:userNameField];

    [self performSelector:@selector(textResponder) withObject:nil afterDelay:0];
    registerButton = [UIButton newAutoLayoutView];
    registerButton.layer.borderWidth = 1;
    registerButton.layer.cornerRadius = 4;
    registerButton.layer.borderColor = [AppCenter color:@"76,187,248,1"].CGColor;
    [registerButton setTitleColor:[AppCenter color:@"76,187,248,1"] forState:UIControlStateNormal];
    [registerButton setTitle:@"下一步" forState:UIControlStateNormal];
    [registerButton addTarget:self action:@selector(registerButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerButton];
    
    _areaArray= [NSMutableArray array];
    
    
    //获取支持的地区列表
    [SMS_SDK getZone:^(enum SMS_ResponseState state, NSArray *array)
     {
         if (1 == state)
         {
             NSLog(@"get the area code sucessfully");
             //区号数据
             _areaArray=[NSMutableArray arrayWithArray:array];
         }
         else if (0 == state)
         {
             NSLog(@"failed to get the area code");
         }
         
     }];

}
-(void)textResponder
{
    [userNameField becomeFirstResponder];
}

-(void)registerButton:(id)sender
{
  
    if (![self verifiPhone])
    {
        UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@""
                                                      message:@"手机号码格式错误"
                                                     delegate:self
                                            cancelButtonTitle:@"知道了"
                                            otherButtonTitles:nil, nil];
        
        [alert show];
        
    }else{
        [self sendMessage];
    }
  
    
}
-(BOOL)verifiPhone
{
    if ([AppCenter string:userNameField.text].length !=11)
    {
        return NO;
    }else
    {
        int compareResult = 0;
        for (int i=0; i<_areaArray.count; i++)
        {
            NSDictionary * dict1 = [_areaArray objectAtIndex:i];
            NSString     * code1 = [dict1 valueForKey:@"zone"];
            if ([code1 isEqualToString:@"86"])
            {
                compareResult=1;
                NSString* rule1=[dict1 valueForKey:@"rule"];
                NSPredicate* pred=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",rule1];
                BOOL isMatch=[pred evaluateWithObject:userNameField.text];
                if (!isMatch)
                {
                    
                    return NO;
                }
                break;
            }
        }
    }
    return  YES;
}


-(void)sendMessage
{
     IdentifyingController * controller = [[IdentifyingController alloc] initWithPhone:[AppCenter string:userNameField.text]];
    
    [SMS_SDK getVerificationCodeBySMSWithPhone:[AppCenter string:userNameField.text] zone:@"86" result:^(SMS_SDKError *error) {
        
        if (error)
        {
            [NotificationNavView showMessage:error.description];
                
        }else
        {
        
           
             [self.navigationController pushViewController:controller animated:YES];
            
        }
        
    }];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
   
}
- (void)viewWillAppear:(BOOL)animated {
    
    [self.navigationController setNavigationBarHidden:NO animated:TRUE];
    
    [super viewWillAppear:animated];
    
    
}

@end
